/**
 * @author htl
 * @date 2018/12/18 0018
 * @Description:
 */

const express = require("express");
const router = express.Router();
const utils = require('../../public/utils');
const {http} = require('../../public/api-helper');
const wechatFirMenuModel = require('../../model/wechat/first-menu-model');
const wechatSecMenuModel = require('../../model/wechat/second-menu-model');
const config = require('../../config');
const wxApiUrl = config.weChatApi;

/*
* 添加一级微信菜单按钮
* @parmas:
*   data: {
*       type: '',  //必填
*       name: '',  //必填
*       key: '',  //必填
*       url: ''   //必填
*   }
* */
router.post('/addFirMenu',async(req,res) => {
    const data = req.body.data;
    const total = await wechatFirMenuModel.getCount();
    if(total >= 3) {
        res.send(utils.setJsonStyle('9998', '公众号菜单最多只支持3个一级菜单，已到达上限，无法继续添加'));
        return;
    }
    const result = wechatFirMenuModel.addDocs(data);
    if(result !== 9999) {
        res.send(utils.setJsonStyle('0', 'success'));
        /*const data = await http(wxApiUrl + '/menu/initMenu'); //初始化微信菜单
        if(data.errcode == 0) {
            res.send(utils.setJsonStyle('0', 'success'));
        }else {
            res.send(utils.setJsonStyle('9998', '微信按钮初始化失败'));
        }*/

    }else {
        res.send(utils.setJsonStyle('9999', '保存失败'));
    }
})

//删除一级微信菜单按钮
router.post('/delFirMenu',async(req,res) => {
    const _id = req.body.id;
    const result = await wechatFirMenuModel.deleteDocs({_id});
    if(result !== 9999) {
        res.send(utils.setJsonStyle('0', 'success'));
    }else {
        res.send(utils.setJsonStyle('9999', '删除失败'));
    }
})

//获取数据库中一级微信菜单列表
router.post('/getFirMenuList',async(req,res) => {
    const list =await wechatFirMenuModel.findDocs();
    res.send(utils.setJsonStyle('0', 'success',list));
})

/*
* 更新一级微信菜单列表
* @params:
*   id: 一级菜单主键
*   data：该主键id的所有数据
* */
router.post('/updateFirMenuList',async(req,res) => {
    const id = req.body.id;
    const data = req.body.data;
    if(!utils.checkObjectId(id)) {
        res.send(utils.setJsonStyle('9999', 'id格式错误'));
        return;
    }
    const result =await wechatFirMenuModel.updateDocs({_id:id},data);
    if(result !== 999) {
        res.send(utils.setJsonStyle('0', 'success'));
        /*const data = await http(wxApiUrl + '/menu/initMenu'); //初始化微信菜单
        if(data.errcode == 0) {
            res.send(utils.setJsonStyle('0', 'success'));
        }else {
            res.send(utils.setJsonStyle('9998', '微信按钮初始化失败'));
        }*/
    }else {
        res.send(utils.setJsonStyle('9999', '更新失败'));
    }

})


/*
* 添加二级微信菜单按钮
* @parmas:
*   data: {
*       pid: '',  //父级主键 （必填）
*       type: '',  //必填
*       name: '',  //必填
*       key: '',  //必填
*       url: ''   //必填
*   }
* */
router.post('/addSecMenu',async(req,res) => {
    const data = req.body.data;
    const pid = req.body.pid;
    const docs = await wechatSecMenuModel.findDocs({pid});
    if(docs.length >= 5) {
        res.send(utils.setJsonStyle('9998', '公众号二级菜单最多只支持5个，数量已到达上限，无法继续添加'));
        return;
    }
    const result = wechatSecMenuModel.addDocs({pid,...data});
    if(result !== 9999) {
        res.send(utils.setJsonStyle('0', 'success'));
        /*const data = await http(wxApiUrl + '/menu/initMenu'); //初始化微信菜单
        if(data.errcode == 0) {
            res.send(utils.setJsonStyle('0', 'success'));
        }else {
            res.send(utils.setJsonStyle('9998', '微信按钮初始化失败'));
        }*/

    }else {
        res.send(utils.setJsonStyle('9999', '保存失败'));
    }
})

//删除二级微信菜单按钮
router.post('/delFirMenu',async(req,res) => {
    const _id = req.body.id;
    const result = await wechatSecMenuModel.deleteDocs({_id});
    if(result !== 9999) {
        res.send(utils.setJsonStyle('0', 'success'));
    }else {
        res.send(utils.setJsonStyle('9999', '删除失败'));
    }
})

/*
* 更新二级微信菜单列表
* @params:
*   id: 二级菜单主键
*   data：该主键id的所有数据
* */
router.post('/updateSecMenuList',async(req,res) => {
    const id = req.body.id;
    const data = req.body.data;
    if(!utils.checkObjectId(id)) {
        res.send(utils.setJsonStyle('9999', 'id格式错误'));
        return;
    }
    const result =await wechatSecMenuModel.updateDocs({_id:id},data);
    if(result !== 999) {
        res.send(utils.setJsonStyle('0', 'success'));
        /*const data = await http(wxApiUrl + '/menu/initMenu'); //初始化微信菜单
        if(data.errcode == 0) {
            res.send(utils.setJsonStyle('0', 'success'));
        }else {
            res.send(utils.setJsonStyle('9998', '微信按钮初始化失败'));
        }*/
    }else {
        res.send(utils.setJsonStyle('9999', '更新失败'));
    }

})

//获取数据库中二级微信菜单列表
router.post('/getFirMenuList',async(req,res) => {
    const list =await wechatSecMenuModel.findDocs();
    res.send(utils.setJsonStyle('0', 'success',list));
})





module.exports = router;